package hs4m.kidseducational.memory;

import hs4m.kidseducational.R;
import hs4m.kidseducational.util.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MemoryActivity extends Activity {
	private static int ROW_COUNT = -1;
	private static int COL_COUNT = -1;
	private Context context;
	private Drawable backImage;
	private int[][] cards;
	private List<Drawable> images;
	private Card firstCard;
	private Card seconedCard;
	private ButtonListener buttonListener;

	private static Object lock = new Object();

	int turns;
	private TableLayout mainTable;
	private UpdateCardsHandler handler;

	int numResolve = 0;
	int goodPoint = 0;
	Button btnOpenDialog;
	MediaPlayer mMedia;

	public boolean isRunning = false;

	/** Play the sound using android.media.MediaPlayer */
	public void playSound(int soundID) {		
		if (mMedia != null) {
			mMedia.reset();
		}		
		mMedia = MediaPlayer.create(context, soundID);
		mMedia.start();		
	}

	@Override
	protected void onDestroy() {
		if (mMedia != null && mMedia.isPlaying()) {
			mMedia.stop();
		}
		super.onDestroy();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		handler = new UpdateCardsHandler();
		setContentView(R.layout.activity_memory);

		// btnOpenDialog = (Button) findViewById(R.id.opendialog);
		// btnOpenDialog.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// openDialog();
		// }
		// });

		// TextView url = ((TextView) findViewById(R.id.myWebSite));
		// Linkify.addLinks(url, Linkify.WEB_URLS);

		// backImage = getResources().getDrawable(R.drawable.icon); // icon

		/*
		 * ((Button)findViewById(R.id.ButtonNew)).setOnClickListener(new
		 * OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { newGame();
		 * 
		 * }
		 * 
		 * 
		 * });
		 */

		buttonListener = new ButtonListener();

		mainTable = (TableLayout) findViewById(R.id.game_layout);

		context = mainTable.getContext();

		// Spinner s = (Spinner) findViewById(R.id.Spinner01);
		// ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
		// R.array.type, android.R.layout.simple_spinner_item);
		// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// s.setAdapter(adapter);

		// s.setOnItemSelectedListener(new OnItemSelectedListener() {
		//
		// @Override
		// public void onItemSelected(android.widget.AdapterView<?> arg0,
		// View arg1, int pos, long arg3) {
		//
		// ((Spinner) findViewById(R.id.Spinner01)).setSelection(0);
		//
		// int x, y;
		//
		// switch (pos) {
		// case 1:
		// x = 4;
		// y = 4;
		// break;
		// case 2:
		// x = 5; // 4
		// y = 4; // 5
		// break;
		// case 3:
		// x = 6; // 4
		// y = 4; // 6
		// break;
		// case 4:
		// x = 7; // 5
		// y = 5; // 6
		// break;
		// case 5:
		// x = 8; // 6
		// y = 5; // 6
		// break;
		// default:
		// return;
		// }
		// newGame(x, y);
		//
		// }
		// @Override
		// public void onNothingSelected(AdapterView<?> arg0) {
		// // TODO Auto-generated method stub
		//
		// }
		// });
		// MinhVT - Cai dat cap do choi
		int cate = (int) getIntent().getIntExtra("cate", 1);
		int level = (int) getIntent().getIntExtra("level", 1);

		switch (cate) {
		case 1:
			backImage = getResources().getDrawable(R.drawable.pokercard); // icon
			loadImagesCate_1();
			break;
		case 2:
			backImage = getResources().getDrawable(R.drawable.ic_launcher); // icon
			loadImagesCate_2();
			break;
		case 3:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_3();
			break;
		case 4:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_4();
			break;
		case 5:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_5();
			break;
		case 6:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_6();
			break;
		case 7:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_7();
			break;
		case 8:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_8();
			break;
		case 9:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_9();
			break;
		default:
			backImage = getResources().getDrawable(R.drawable.icon); // icon
			loadImagesCate_1();
			break;
		}

		// Level
		switch (level) {
		case 1:
			// newGame(4, 4);
			// numResolve = 16 / 2;
			newGame(4, 4);
			goodPoint = numResolve = 16 / 2;
			break;
		case 2:
			newGame(5, 4);
			goodPoint = numResolve = 20 / 2;
			break;
		case 3:
			newGame(6, 4);
			goodPoint = numResolve = 24 / 2;
			break;
		case 4:
			newGame(6, 5);
			goodPoint = numResolve = 30 / 2;
			break;
		case 5:
			newGame(8, 5);
			goodPoint = numResolve = 40 / 2;
			break;
		default:
			newGame(4, 4);
			goodPoint = numResolve = 16 / 2;
			break;
		}
	}

	private void newGame(int c, int r) {
		ROW_COUNT = r;
		COL_COUNT = c;

		cards = new int[COL_COUNT][ROW_COUNT];

		mainTable.removeView(findViewById(R.id.TableRow01));
		mainTable.removeView(findViewById(R.id.TableRow02));

		TableRow tr = ((TableRow) findViewById(R.id.TableRow03));
		tr.removeAllViews();

		mainTable = new TableLayout(context);
		tr.addView(mainTable);

		for (int y = 0; y < ROW_COUNT; y++) {
			mainTable.addView(createRow(y));
		}

		firstCard = null;
		loadCards();

		turns = 0;
		((TextView) findViewById(R.id.tv1)).setText("Tries: " + turns);

	}

	// Cate 1
	private void loadImagesCate_1() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	// Cate 2
	private void loadImagesCate_2() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.animal1));
		images.add(getResources().getDrawable(R.drawable.animal2));
		images.add(getResources().getDrawable(R.drawable.animal3));
		images.add(getResources().getDrawable(R.drawable.animal4));
		images.add(getResources().getDrawable(R.drawable.animal5));
		images.add(getResources().getDrawable(R.drawable.animal6));
		images.add(getResources().getDrawable(R.drawable.animal7));
		images.add(getResources().getDrawable(R.drawable.animal8));
		images.add(getResources().getDrawable(R.drawable.animal9));
		images.add(getResources().getDrawable(R.drawable.animal10));
		images.add(getResources().getDrawable(R.drawable.animal11));
		images.add(getResources().getDrawable(R.drawable.animal12));
		images.add(getResources().getDrawable(R.drawable.animal13));
		images.add(getResources().getDrawable(R.drawable.animal14));
		images.add(getResources().getDrawable(R.drawable.animal15));
		images.add(getResources().getDrawable(R.drawable.animal16));
		images.add(getResources().getDrawable(R.drawable.animal17));
		images.add(getResources().getDrawable(R.drawable.animal18));
		images.add(getResources().getDrawable(R.drawable.animal19));
		images.add(getResources().getDrawable(R.drawable.animal20));
		images.add(getResources().getDrawable(R.drawable.animal21));

	}

	// Cate 3
	private void loadImagesCate_3() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	// Cate 4
	private void loadImagesCate_4() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	// Cate 5
	private void loadImagesCate_5() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	// Cate 6
	private void loadImagesCate_6() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	// Cate 7
	private void loadImagesCate_7() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	// Cate 8
	private void loadImagesCate_8() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	// Cate 9
	private void loadImagesCate_9() {
		images = new ArrayList<Drawable>();

		images.add(getResources().getDrawable(R.drawable.card1));
		images.add(getResources().getDrawable(R.drawable.card2));
		images.add(getResources().getDrawable(R.drawable.card3));
		images.add(getResources().getDrawable(R.drawable.card4));
		images.add(getResources().getDrawable(R.drawable.card5));
		images.add(getResources().getDrawable(R.drawable.card6));
		images.add(getResources().getDrawable(R.drawable.card7));
		images.add(getResources().getDrawable(R.drawable.card8));
		images.add(getResources().getDrawable(R.drawable.card9));
		images.add(getResources().getDrawable(R.drawable.card10));
		images.add(getResources().getDrawable(R.drawable.card11));
		images.add(getResources().getDrawable(R.drawable.card12));
		images.add(getResources().getDrawable(R.drawable.card13));
		images.add(getResources().getDrawable(R.drawable.card14));
		images.add(getResources().getDrawable(R.drawable.card15));
		images.add(getResources().getDrawable(R.drawable.card16));
		images.add(getResources().getDrawable(R.drawable.card17));
		images.add(getResources().getDrawable(R.drawable.card18));
		images.add(getResources().getDrawable(R.drawable.card19));
		images.add(getResources().getDrawable(R.drawable.card20));
		images.add(getResources().getDrawable(R.drawable.card21));

	}

	private void loadCards() {
		try {
			int size = ROW_COUNT * COL_COUNT;

			Log.i("loadCards()", "size=" + size);

			ArrayList<Integer> list = new ArrayList<Integer>();

			for (int i = 0; i < size; i++) {
				list.add(new Integer(i));
			}

			Random r = new Random();

			for (int i = size - 1; i >= 0; i--) {
				int t = 0;

				if (i > 0) {
					t = r.nextInt(i);
				}

				t = list.remove(t).intValue();
				cards[i % COL_COUNT][i / COL_COUNT] = t % (size / 2);

				Log.i("loadCards()", "card[" + (i % COL_COUNT) + "]["
						+ (i / COL_COUNT) + "]="
						+ cards[i % COL_COUNT][i / COL_COUNT]);
			}
		} catch (Exception e) {
			Log.e("loadCards()", e + "");
		}

	}

	private TableRow createRow(int y) {
		TableRow row = new TableRow(context);
		row.setHorizontalGravity(Gravity.CENTER);

		for (int x = 0; x < COL_COUNT; x++) {
			row.addView(createImageButton(x, y));
		}
		return row;
	}

	private View createImageButton(int x, int y) {
		Button button = new Button(context);
		button.setBackgroundDrawable(backImage);
		button.setId(100 * x + y);
		button.setOnClickListener(buttonListener);
		return button;
	}

	class ButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			playSound(R.raw.mario_jumping);
			// if(mMedia != null){
			// mMedia.reset();
			// }
			// mMedia = MediaPlayer.create(getApplicationContext(),
			// R.raw.mario_jumping);
			// mMedia.start();
			synchronized (lock) {
				if (firstCard != null && seconedCard != null) {
					return;
				}
				int id = v.getId();
				int x = id / 100;
				int y = id % 100;
				turnCard((Button) v, x, y);
			}

		}

		private void turnCard(Button button, int x, int y) {
			button.setBackgroundDrawable(images.get(cards[x][y]));

			if (firstCard == null) {
				firstCard = new Card(button, x, y);
			} else {

				if (firstCard.x == x && firstCard.y == y) {
					return; // the user pressed the same card
				}

				seconedCard = new Card(button, x, y);

				turns++;
				((TextView) findViewById(R.id.tv1)).setText("Tries: " + turns);

				TimerTask tt = new TimerTask() {

					@Override
					public void run() {
						try {
							synchronized (lock) {
								handler.sendEmptyMessage(0);
							}
						} catch (Exception e) {
							Log.e("E1", e.getMessage());
						}
					}
				};

				Timer t = new Timer(false);
				t.schedule(tt, 888);
			}

		}

	}

	class UpdateCardsHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			synchronized (lock) {
				checkCards();
			}
		}

		public void checkCards() {
			if (cards[seconedCard.x][seconedCard.y] == cards[firstCard.x][firstCard.y]) {
				firstCard.button.setVisibility(View.INVISIBLE);
				seconedCard.button.setVisibility(View.INVISIBLE);
				numResolve--;
				if (numResolve <= 0) {
					// Dialog imageDiaglog= new
					// Dialog(MemoryActivity.this,R.style.PauseDialog);
					// Dialog imageDiaglog = new Dialog(MemoryActivity.this);
					// imageDiaglog.setTitle(R.string.app_name);
					// imageDiaglog.getWindow().getAttributes().windowAnimations
					// = R.anim.grow_from_left;
					// // imageDiaglog.setContentView(R.layout.pause_menu);
					// imageDiaglog.show();
					playSound(R.raw.handclap_1);
					openDialog();
					// Toast.makeText(context, "Chúc mừng bạn đã hoàn thành",
					// Toast.LENGTH_LONG).show();
				} else {
					playSound(R.raw.complete);
				}
			} else {
				playSound(R.raw.mario_jumping);
				seconedCard.button.setBackgroundDrawable(backImage);
				firstCard.button.setBackgroundDrawable(backImage);
			}

			firstCard = null;
			seconedCard = null;
		}
	}

	private void openDialog() {
		final Dialog dialog = new Dialog(MemoryActivity.this,
				android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

		int point1 = goodPoint * 3;
		int point2 = goodPoint * 2;

		// dialog.setTitle("Kids Education");
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_layout);
		dialog.setCancelable(false);
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

		ImageButton btnMenu = (ImageButton) dialog.getWindow().findViewById(
				R.id.btnMenu);
		btnMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Go to Menu
				finish();
			}
		});
		ImageButton btnDismiss = (ImageButton) dialog.getWindow().findViewById(
				R.id.dismiss);
		btnDismiss.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				// Refresh
				finish();
				startActivity(getIntent());
			}
		});

		// An Sao di
		ImageView star1 = (ImageView) dialog.getWindow().findViewById(
				R.id.Star1);
		ImageView star2 = (ImageView) dialog.getWindow().findViewById(
				R.id.Star2);
		ImageView star3 = (ImageView) dialog.getWindow().findViewById(
				R.id.Star3);

		if (turns >= point1) {
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.GONE);
			star3.setVisibility(View.GONE);
		} else if (point2 <= turns && turns < point1) {
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			star3.setVisibility(View.GONE);
		} else if (turns < point2) {
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			star3.setVisibility(View.VISIBLE);
		}

		dialog.show();
	}

}
